<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
    <script type="text/javascript" src="resources/js/lib/jquery2.js"></script>
    <link rel="stylesheet" type="text/css" href="resources/css/admin/uploadify.css"/>

    <link rel="stylesheet" type="text/css" href="resources/css/common.css"/>
    <script type="text/javascript" src="resources/js/lib/core/jquery.uploadify.min.js"></script>
    <script type="text/javascript" src="resources/js/xheditor-1.1.14-zh-cn.js"></script>
    <script src="resources/js/lib/sea.js"></script>
    <script type="text/javascript">
        seajs.config({
            base: "${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/resources/"
        });
        seajs.use([ "css/admin/jqueryui.custom.css", "css/admin/main.css",
            "css/admin/jquery.cms.keywordinput.css",
            "css/zTreeStyle/zTreeStyle.css",
            "js/lib/core/jquery.cms.tree",
            "css/admin/article.css",
            "css/validate/jquery.validator.css"
            , "js/cms.validate.AddTopic.js"
            , "js/cms.topic.add.js","js/lib/core/jquery.cmsValidate.js"]);

        $(function () {
            var xheditor = $("#content").xheditor({tools: "full"});
            var ctx = $("#ctx"); //上下文路径
            $("#attach").uploadify({
                swf: ctx.val() + "/resources/css/admin/uploadify.swf",
                fileTypeExts: "*.jpg; *.gif;*.png;*.doc;*.txt;*.xls;*.xlsx;*.rar;*.zip;*.pdf,*.flv;*.swf",
                uploader: ctx.val() + "/admin/topic/upload",
                fileObjName: "attach",
                auto: false,
                formData: {"sid": $("#sid").val()},
                onUploadSuccess: function (file, data, response) {
                    var ao = $.parseJSON(data);
                    //1代表上传成功
                    if (ao.result == 1) {
                        var node = createAttachmentNode(ao.obj);
                        $("#ok_attach").find("tbody").append(node);
                    }

                }
            });
            function createAttachmentNode(att) {
                var node = "<tr>";
                if (att.img) {
                    node += "<td><img src='" + $("#ctx").val() + "/resources/upload/thumbnail/" + att.newName + "'></td>"
                } else {
                    node += "<td>普通类型附件</td>";
                }
                node += "<td>" + att.oldName + "</td>";
                node += "<td>" + Math.round((att.size / 1024)) + "KB</td>";
                if (att.img) {

                    node += "<td><input type='checkbox' id='' value='" + att.id + "' name='indexPic' class='indexPic'></td>";
                    node += "<td><input type='radio' id='' value='" + att.id + "' name='channelPicId'></td>";
                } else {

                    node += "<td>&nbsp;</td>";
                    node += "<td>&nbsp;</td>";
                }

                node += "<td><input type='checkbox' value='" + att.id + "' name='attch' class='isAttach'></td>";
                node += "<td><a href='#' class='list_op insertAttach' title='" + att.id + "' isImg='" + att.img + "' name='" + att.newName + "' oldName='"+att.oldName+"'>插入附件</a><a href ='' title='" + att.id + "' class='list_op deleteAttach delete'>删除附件</a></td>";
                node += "<input type='hidden' name='aids' value='"+att.id+"'/>";
                node += "</tr>"

                return node;
            }

            $("#ok_attach").on("click", ".indexPic", function () {
                $.post(ctx.val() + "/admin/topic/updateIndexPic", {"aid": $(this).val()});
            });
            $("#ok_attach").on("click", ".insertAttach", function (event) {
                event.preventDefault();
                var node = "";
                var srcatt = ctx.val()+"/resources/upload/" + $(this).attr("name");
                var strctitle = $(this).attr("title");
                if ($(this).attr("isimg")==1) {
                    node = "<img src ='" + srcatt + "'  id='attach_" + strctitle + "'>";
                } else {
                    node = "<a href='" + srcatt + "' id='attach_" + strctitle + "'>"+$(this).attr("oldName")+"</a>"
                }
                xheditor.pasteHTML(node);
            });

            $("#ok_attach").on("click", ".isAttach", function () {
                $.post(ctx.val() + "/admin/topic/updateAttachInfo", {"aid": $(this).val()});
            });


            $("#ok_attach").on("click", ".deleteAttach", function (event) {
                event.preventDefault();
                var conf = confirm("确定要删除附件么");
                if(conf)  {
                    var ad = this;
                    var ide = $(this).attr("title");
                    $.post("admin/topic/deleteAtt", {"aid": ide}, function (data) {
                        if (data.result == 1) {

                            $(ad).parent("td").parent("tr").remove();
                            console.info( $("#xhe0_iframe").contents().find("#attach_"+ide));
                            $("#xhe0_iframe").contents().find("#attach_"+ide).remove();
                        }
                    }, "json");
                }

            });

            $("#upload").click(function () {
                $("#attach").uploadify("upload", "*");
            });
        });
    </script>
</head>
<body>
<input type="hidden" id="sid" name="sid" value="<%=session.getId()%>"/>

<div id="menuContent" class="menuContent"
     style="display:none; position: absolute;background:#eee;z-index: 999;border:1px solid #999">
    <ul id="mytree" class="ztree" style="margin-top:0;"></ul>
</div>
<div id="container">
    <jsp:include page="/jsp/admin/top_inc.jsp"/>

    <div id="contents">
        <input type="hidden" id="ctx" value="<%=request.getContextPath() %>"/>


        <sf:form method="post" modelAttribute="topicDto" id="addForm">
            <table width="980" cellspacing="0" cellPadding="0" id="addTable">
                <tr>
                    <td class="rightTd" width="120px">文章标题:</td>
                    <td class="leftTd">
                        <sf:input path="title" size="80"/><sf:errors cssClass="errorContainer" path="title"/></td>
                </tr>
                <tr>
                    <td class="rightTd">文章栏目:</td>
                    <td class="leftTd">
                        <input type="text" name="cname" id="cname"/>
                        <input type="hidden" id="cid" name="cid" />
                        <sf:errors cssClass="errorContainer" path="cid"/>
                    </td>
                </tr>
                <c:choose>
                    <c:when test="${isAudit||isAdmin }">
                        <tr>
                            <td class="rightTd">文章状态:</td>
                            <td class="leftTd">
                                <sf:radiobutton path="status" value="0"/>未发布
                                <sf:radiobutton path="status" value="1"/>已发布
                            </td>
                        </tr>
                    </c:when>
                    <c:otherwise>
                        <sf:hidden path="status"/>
                    </c:otherwise>
                </c:choose>
                <tr>
                    <td class="rightTd">是否推荐该文章:</td>
                    <td class="leftTd">
                        <sf:radiobutton path="recommend" value="0"/>不推荐
                        <sf:radiobutton path="recommend" value="1"/>推荐
                    </td>
                </tr>
                <tr>
                    <td class="rightTd">发布时间:</td>
                    <td class="leftTd">
                        <sf:input path="publishDate"/>
                    </td>
                </tr>
                <tr>
                    <td class="rightTd">文章关键字:</td>
                    <td class="leftTd">
                        <sf:input path="keyword"/>
                    </td>
                </tr>
                <tr>
                    <td class="rightTd">文章附件</td>
                    <td class="leftTd">
                        <div id="attachs"></div>
                        <input type="file" id="attach" name="attach"/>
                        <input type="button" id="upload" value="上传文件"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">已传附件</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <table id="ok_attach" width="890px" cellpadding="0" cellspacing="0">
                            <thead>
                            <tr>
                                <td>文件名缩略图</td>
                                <td width="180">文件名</td>
                                <td>文件大小</td>
                                <td>主页图片</td>
                                <td>栏目图片</td>
                                <td>附件信息</td>
                                <td width="160">操作</td>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">文章内容</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <sf:textarea path="content" rows="25" cols="110" name="content" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">文章摘要</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <sf:textarea path="summary" rows="5" cols="110" name="summary"/>

                    </td>
                </tr>
                <tr>
                    <td colspan="2" class="centerTd"><input type="submit"  id="addBtn"
                                                            value="添加文章"/><input
                            type="reset"/></td>
                </tr>
            </table>
        </sf:form>
    </div>
</div>
<script type="text/javascript">


</script>
</body>
</html>